package com.hkfrich.system.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkfrich.common.constant.Constants;
import com.hkfrich.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkfrich.common.utils.StringUtils;
import com.hkfrich.system.domain.SysRole;
import com.hkfrich.system.domain.SysRoleMenu;
import com.hkfrich.system.mapper.SysRoleMapper;
import com.hkfrich.system.mapper.SysRoleMenuMapper;
import com.hkfrich.system.mapper.SysUserRoleMapper;
import com.hkfrich.system.service.SysRoleService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色Service业务层处理
 * 
 * @author _Heishuai
 * @date 2022-09-20 20:54:13
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public String checkRoleNameUnique(SysRole sysRole) {
        Long roleId = StringUtils.isNull(sysRole.getRoleId()) ? -1L : sysRole.getRoleId();
        SysRole info = sysRoleMapper.checkRoleNameUnique(sysRole.getRoleName());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }

    @Override
    public String checkRoleKeyUnique(SysRole sysRole) {
        Long roleId = StringUtils.isNull(sysRole.getRoleId()) ? -1L : sysRole.getRoleId();
        SysRole info = sysRoleMapper.checkRoleKeyUnique(sysRole.getRoleKey());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }

    @Override
    public int updateRole(SysRole sysRole) {
        // 修改角色信息
        sysRoleMapper.updateById(sysRole);
        // 删除角色与菜单关联
        sysRoleMenuMapper.deleteRoleMenuByRoleId(sysRole.getRoleId());
        return insertRoleMenu(sysRole);
    }

    @Override
    public int deleteRoleByIds(List<Long> roleIds) {
        for (Long roleId : roleIds)
        {
            SysRole role = sysRoleMapper.selectRoleById(roleId);
            if (sysUserRoleMapper.countUserRoleByRoleId(roleId) > 0)
            {
                throw new RuntimeException(String.format("%1$s已分配,不能删除", role.getRoleName()));
            }
        }
        // 删除角色与菜单关联
        sysRoleMenuMapper.deleteRoleMenu(roleIds);
        return sysRoleMapper.deleteRoleByIds(roleIds);
    }



    public int insertRoleMenu(SysRole sysRole) {
        int rows = 1;
        // 新增用户与角色管理
        List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
        for (Long menuId : sysRole.getMenuIds())
        {
            SysRoleMenu rm = new SysRoleMenu();
            rm.setRoleId(sysRole.getRoleId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = sysRoleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }
}
